package org.ant.gui;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.ant.ctrl.CodeErrorManager;
import org.ant.ctrl.CodesManager;
import org.ant.gui.bar.AntToolBar;
import org.ant.gui.bar.FloorBar;
import org.ant.gui.layout.GBC;
import org.ant.gui.menu.AntMenuBar;
import org.ant.gui.panes.AssTabbedPane;
import org.ant.gui.panes.InfTabbedPane;
import org.ant.gui.panes.InputTabbedPane;
import org.ant.gui.panes.SideTabbedPane;
import org.ant.properties.PropertyManager;
import org.jvnet.substance.skin.SubstanceNebulaBrickWallLookAndFeel;

public class AntFrame extends JFrame implements WindowListener {

	private static AntFrame antFrame = new AntFrame();

	private static final String FRAME_ICON = "icon/frame_icon.gif";
	private static final String TITILE = "Queen Ant";

	private static final int INSETS = 2;

	private PropertyManager pm;

	private BufferedImage icon;
	private AntToolBar toolBar;
	private AntMenuBar menuBar;
	private FloorBar floorBar;
	private AssTabbedPane assTabbedPane;
	private InfTabbedPane infTabbedPane;
	private InputTabbedPane inputTabbedPane;
	private SideTabbedPane sideTabbedPane;

	public static AntFrame getInstance() {
		return antFrame;
	}

	private AntFrame() {
		super();
		init();
		loadComponents();
		adjustFrame();
	}

	protected void init() {
		try {
			icon = ImageIO.read(new File(FRAME_ICON));
			setIconImage(icon);
		} catch (IOException e) {
			e.printStackTrace();
		}

		setTitle(TITILE);
		setStyle();
		setResizable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		pm = PropertyManager.getInstance();
	}

	protected void loadComponents() {
		setLayout(new GridBagLayout());

		setJMenuBar(getAntMenuBar());
		add(getToolBar(), new GBC(0, 0, 3, 1).setWeight(0, 0).setAnchor(
				GBC.NORTH).setFill(GBC.HORIZONTAL).setInset(INSETS));
		add(getSideTabbedPane(), new GBC(0, 1, 1, 2).setWeight(50, 100)
				.setAnchor(GBC.WEST).setFill(GBC.BOTH).setInset(INSETS));
		add(getAssTabbedPane(), new GBC(2, 1, 1, 1).setWeight(40, 100)
				.setAnchor(GBC.EAST).setFill(GBC.BOTH).setInset(INSETS));
		add(getInfTabbedPane(), new GBC(1, 2, 2, 1).setWeight(100, 40)
				.setAnchor(GBC.SOUTH).setFill(GBC.BOTH).setInset(INSETS));
		add(getFloorBar(), new GBC(0, 3, 3, 1).setWeight(0, 0).setAnchor(
				GBC.SOUTH).setFill(GBC.HORIZONTAL).setInset(INSETS));
		add(getInputTabbedPane(), new GBC(1, 1, 1, 1).setWeight(75, 100)
				.setAnchor(GBC.CENTER).setFill(GBC.BOTH).setInset(INSETS));
		
	}

	protected void adjustFrame() {
		pack();
		Rectangle maxmizedRectangle = GraphicsEnvironment
				.getLocalGraphicsEnvironment().getMaximumWindowBounds();
		setMaximizedBounds(maxmizedRectangle);
		setBounds(new Rectangle(800, 600));
		setExtendedState(JFrame.MAXIMIZED_BOTH);
	}

	public AntMenuBar getAntMenuBar() {
		if (menuBar == null) {
			menuBar = new AntMenuBar();
		}
		return menuBar;
	}

	public AntToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new AntToolBar();
		}
		return toolBar;
	}

	public FloorBar getFloorBar() {
		if (floorBar == null) {
			floorBar = new FloorBar();
		}
		return floorBar;
	}

	public AssTabbedPane getAssTabbedPane() {
		if (assTabbedPane == null) {
			assTabbedPane = new AssTabbedPane();
			CodesManager.getInstane().addActionListener(assTabbedPane);
		}
		return assTabbedPane;
	}

	public InfTabbedPane getInfTabbedPane() {
		if (infTabbedPane == null) {
			infTabbedPane = new InfTabbedPane();
			// CodesManager.getInstane().addActionListener(assTabbedPane);
		}
		return infTabbedPane;
	}

	public InputTabbedPane getInputTabbedPane() {
		if (inputTabbedPane == null) {
			inputTabbedPane = new InputTabbedPane();
			CodesManager.getInstane().addActionListener(inputTabbedPane);
		}
		return inputTabbedPane;
	}

	public SideTabbedPane getSideTabbedPane() {
		if (sideTabbedPane == null) {
			sideTabbedPane = new SideTabbedPane();
			CodesManager.getInstane().addActionListener(sideTabbedPane);
		}
		return sideTabbedPane;
	}

	private void setStyle() {
		try {
			initGlobalFontSetting(new Font(null, Font.PLAIN, 14));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initGlobalFontSetting(Font font) {
		FontUIResource fontRes = new FontUIResource(font);
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys
				.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource)
				UIManager.put(key, fontRes);
		}
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					UIManager
							.setLookAndFeel(new SubstanceNebulaBrickWallLookAndFeel ());
					SwingUtilities
							.updateComponentTreeUI(AntFrame.getInstance());
				} catch (Exception e) {
					e.printStackTrace();
				}
				AntFrame.getInstance().setVisible(true);
				CodeErrorManager.addError("SS+\n+ss");

			}
		});
	}
}
